home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / db / esm-3.1 / esm-3 / usr / local / sm / src / serverlib / thread / sleep.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-05  |  3.8 KB  |  140 lines

  1. /*
  2.  *   $RCSfile: sleep.C,v $  
  3.  *   $Revision: 1.1.1.1 $  
  4.  *   $Date: 1996/05/04 21:56:02 $      
  5.  */ 
  6. /**********************************************************************
  7. * EXODUS Database Toolkit Software
  8. * Copyright (c) 1991 Computer Sciences Department, University of
  9. *                    Wisconsin -- Madison
  10. * All Rights Reserved.
  11. *
  12. * Permission to use, copy, modify and distribute this software and its
  13. * documentation is hereby granted, provided that both the copyright
  14. * notice and this permission notice appear in all copies of the
  15. * software, derivative works or modified versions, and any portions
  16. * thereof, and that both notices appear in supporting documentation.
  17. *
  18. * THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY OF WISCONSIN --
  19. * MADISON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.  
  20. * THE DEPARTMENT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
  21. * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  22. *
  23. * The EXODUS Project Group requests users of this software to return 
  24. * any improvements or extensions that they make to:
  25. *
  26. *   EXODUS Project Group 
  27. *     c/o David J. DeWitt and Michael J. Carey
  28. *   Computer Sciences Department
  29. *   University of Wisconsin -- Madison
  30. *   Madison, WI 53706
  31. *
  32. *     or exodus@cs.wisc.edu
  33. *
  34. * In addition, the EXODUS Project Group requests that users grant the 
  35. * Computer Sciences Department rights to redistribute these changes.
  36. **********************************************************************/
  37.  
  38. #include "sysdefs.h"
  39. #include "ess.h"
  40. #include "checking.h"
  41. #include "trace.h"
  42. #include "error.h"
  43. #include "list.h"
  44. #include "tid.h"
  45. #include "io.h"
  46. #include "lock.h"
  47. #include "thread.h"
  48. #include "timer.h"
  49. #include "thread_funcs.h"
  50. #include "thread_globals.h"
  51. #include "threadstate.h"
  52.  
  53.  
  54.  
  55. /*
  56.  * Wakeup is to be used for threads that are sleeping.
  57.  * It does not work to try to wake up a thread that
  58.  * is waiting on a semaphore or some other such thing.
  59.  * For those, you MUST use notify().
  60.  */
  61.  
  62. BOOL AwakenedAThread = FALSE;
  63.  
  64. void
  65. WakeUp( TCB *who )
  66. {
  67.  
  68.     TRPRINT(TR_THREAD, TR_LEVEL_1, ("WakeUp: thread %d, state %d", 
  69.         who->id, who->state));
  70.  
  71.     /*
  72.      *  Unlike with semaphores,  this
  73.      *    thread is not on a list.
  74.      *  Only one thread can be awaiting a given 
  75.      *  timer at any time.
  76.      */
  77.     
  78.     /*  This can get called by the timer or by anyone.
  79.      *  If the timer calls us and we have already been awakened,
  80.      *  we had better not barf.
  81.      */
  82.     if( who->state != THREAD_WAKEUP_WAIT )  {
  83.         /* it is ok - do not barf - just ignore the wakeup */
  84.         TRPRINT(TR_THREAD, TR_LEVEL_1, ("wakeup ignored"));
  85.         return;
  86.     }
  87.     TRPRINT(TR_THREAD, TR_LEVEL_1, ("waking up thread %d", who->id));
  88.  
  89.     AwakenedAThread = TRUE;
  90.     listEnq( &ReadyList, &(who->controlList) );
  91. }
  92.  
  93. void
  94. Sleep( int seconds )
  95. {
  96.     int newThreadState;
  97.  
  98.     SM_ASSERT(LEVEL_3, NOT_LIST_MEMBER(&(Active->controlList)));
  99.  
  100.     switch( seconds ) {
  101.     case SLEEP_DONT_SLEEP:
  102.         TRPRINT(TR_THREAD, TR_LEVEL_1, 
  103.             ("thread %d going to back of ready Q", Active->id));
  104.         /* 
  105.          * Just put the current thread at the back of the ready list.
  106.          * This thread will NOT be awakened by Wakeup ().
  107.         */
  108.  
  109.         listEnq( &ReadyList, &(Active->controlList) );
  110.         newThreadState = THREAD_READY_WAIT;
  111.         break;
  112.  
  113.     default:
  114.         TRPRINT(TR_THREAD, TR_LEVEL_1, 
  115.             ("thread %d setting timer for %d seconds", Active->id, seconds));
  116.  
  117.         ServerTimer.Start( (TIME)seconds,  
  118.             (TIMERFUNC) WakeUp, (TIMERARG) Active );
  119.         Active->error = esmNOERROR;
  120.         /* drop through: */
  121.  
  122.     case SLEEP_INDEFINITELY:
  123.         TRPRINT(TR_THREAD, TR_LEVEL_1, 
  124.             ("thread %d sleeping %s", Active->id,
  125.                 seconds==SLEEP_INDEFINITELY?"indefinitely":""));
  126.         /* 
  127.          * This thread must be awakened by Wakeup().
  128.          * It is not going on an waitlist.
  129.          * The thread that wakes us up must have a handle
  130.          * on this tcb, probably by getting it from a net message
  131.          * or a disk message.
  132.          */
  133.         newThreadState = THREAD_WAKEUP_WAIT;
  134.         break;
  135.     }
  136.     dispatch( newThreadState ); 
  137. }
  138.  
  139.  
  140.